const { axios } = require('../../utils/http')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify.js'

Page({
  data: {
    email: '',
    code: ''
  },
  changeEmail() {
    if (this.data.email.trim() === '') {
      Notify({
        message: '请输入新邮箱'
      })
      return
    }
    const userInfo = wx.getStorageSync('userInfo')
    axios
      .post('/user/validEmailCode', {
        email: this.data.email,
        id: userInfo.id,
        code: this.data.code
      })
      .then(res => {
        console.log(res)
        if (res.data.error === -1) {
          wx.setStorageSync('userInfo', {
            ...userInfo,
            email: this.data.email
          })
          wx.navigateBack()
        } else {
          Notify({
            message: res.data.data
          })
        }
      })
  },
  sendEmailCode() {
    if (this.data.email === '') {
      Notify({
        message: '请输入新邮箱'
      })
      return
    }
    const userInfo = wx.getStorageSync('userInfo')
    axios
      .post('/user/sendEmailCode', {
        id: userInfo.id,
        email: this.data.email
      })
      .then(res => {
        console.log(res)
        if (res.data.error === -1) {
          Notify({
            message: '发送邮件成功',
            type: 'success'
          })
        } else {
          Notify({
            message: '发送邮件失败'
          })
        }
      })
  }
})
